package chapter4.No4_1_使用reentrantLock类.demo9;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2016/11/21.
 */
public class ServiceClass {
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    volatile boolean flag = false;


    public void set() {
        try {
            lock.lock();
            if (flag == false) {
                System.out.println("set 进入 waiting状态");
                condition.await();
            }
            System.out.println("打印☆");
            flag = false;
            condition.signal();
            lock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void get() {
        try {
            lock.lock();
            if (flag == true) {
                System.out.println("get 进入 waiting状态");
                condition.await();
            }
            System.out.println("打映★");
            flag = true;
            condition.signal();
            lock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
